package com.xyc.petmallsystem.repository;

import com.xyc.petmallsystem.pojo.Cars;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Joel
 * @date 2021/12/2
 */
public interface CarsRepository extends JpaRepository<Cars, Integer> {

    /*
    根据购物车的id查询cars
     */
    @Query(value = "select *  from cars where id = :id", nativeQuery = true)
    Cars findCatCount(Integer id);

    /*
    获取购物车数量
     */
    @Query(value = "select count(*) from cars", nativeQuery = true)
    Integer getCarsCount();

    /*
    根据用户 id获取购物车数量
     */
    @Query(value = "select count(*) from cars where user_id = :id", nativeQuery = true)
    Integer getCarsCount2(Integer id);

    /*
    根据用户的id获取购物车所有信息
     */
    @Query(value = "select * from cars where user_id = :id", nativeQuery = true)
    List<Cars> getCarsFindById(Integer id);

    /*
    根据 user_id删除
     */
    @Modifying
    @Transactional
    @Query(value = "delete from cars where user_id = :user_id", nativeQuery = true)
    void delByUserId(Integer user_id);
}
